package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.cloud.common.config.servlet.JwtUtils;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.vo.PageReq;
import com.dd.cloud.user.entity.doctor.PatientMedCard;
import com.dd.cloud.user.entity.patient.PatientUserAddress;
import com.dd.cloud.user.mq.MQConfig;
import com.dd.cloud.user.mq.RabbitSend;
import com.dd.cloud.user.req.patient.*;
import com.dd.cloud.user.res.patient.PatientMedCardBindRes;
import com.dd.cloud.user.res.patient.PatientUserAppraiseRes;
import com.dd.cloud.user.res.patient.PatientUserComplaintRes;
import com.dd.cloud.user.res.patient.PatientUserWxLoginRes;
import com.dd.cloud.user.res.plat.PlatLoginRes;
import com.dd.cloud.user.service.doctor.IDoctorPatientBindService;
import com.dd.cloud.user.service.patient.*;
import com.dd.cloud.user.vo.DelayMqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @Description: 患者端controller
 * @Author: czg
 * @Date: 2023/7/12 10:22
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/patient")
@Slf4j
@Validated
public class PatientController {
    @Autowired
    private IPatientUserService patientUserService;
    @Autowired
    private IPatientMedRankService patientMedRankService;
    @Autowired
    private IDoctorPatientBindService doctorPatientBindService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private IPatientUserAppraiseService patientUserAppraiseService;
    @Autowired
    private IPatientUserComplaintService patientUserComplaintService;
    @Autowired
    private IPatientUserAddressService patientUserAddressService;

    /**
     * 查询患者绑定的就诊人
     */
    @GetMapping(value = "/med/bind/list")
    public ResponseEntity<Map<Integer, List<PatientMedCardBindRes>>> bindMedList() {
        Map<Integer, List<PatientMedCardBindRes>> res = patientMedRankService.bindMedList();
        return ResponseEntity.ok(res);
    }

    /**
     * 查询最后一次使用的就诊卡
     */
    @GetMapping(value = "/med/last/use")
    public ResponseEntity<PatientMedCardBindRes> lastUseMed() {
        PatientMedCardBindRes res = patientMedRankService.lastUseMed();
        return ResponseEntity.ok(res);
    }

    /**
     * 患者选择就诊人
     *
     * @param medId 就诊人的id
     */
    @GetMapping(value = "/med/set/use")
    public ResponseEntity<Void> setUseMed(@NotBlank(message = "就诊人的medId不能为空") String medId) {
        patientMedRankService.setUseMed(medId);
        return ResponseEntity.ok().build();
    }

    /**
     * id查询就诊人
     *
     * @param id 就诊卡的id
     */
    @GetMapping(value = "/med/by/id")
    public ResponseEntity<PatientMedCard> medById(@NotNull(message = "就诊人的id不能为空") Integer id) {
        PatientMedCard patientMedCard = patientMedRankService.medById(id);
        return ResponseEntity.ok(patientMedCard);
    }

    /**
     * 患者添加就诊卡
     */
    @PostMapping(value = "/med/add")
    public ResponseEntity<Void> medAdd(@RequestBody PatientMedCardAddReq req) {
        log.info("患者添加就诊卡:{}", req);
        patientMedRankService.medAdd(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 患者修改就诊卡
     */
    @PostMapping(value = "/med/update")
    public ResponseEntity<Void> medUpdate(@RequestBody PatientMedCardUpdateReq req) {
        log.info("患者修改就诊卡:{}", req);
        patientMedRankService.medUpdate(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 绑定医生患者关系
     *
     * @param doctorId
     * @param medId
     */
    @PostMapping(value = "/doctor/bind")
    public ResponseEntity<Void> doctorPatientBind(@NotNull(message = "医生id不能为空") Integer doctorId,
                                                  @NotBlank(message = "就诊卡id不能为空") String medId) {
        doctorPatientBindService.doctorPatientBind(doctorId, medId, 2);
        return ResponseEntity.ok().build();
    }

    /**
     * 患者解绑就诊卡
     */
    @PostMapping(value = "/med/bind/relieve")
    public ResponseEntity<Void> medBindRelieve(Integer id) {
        patientMedRankService.medBindRelieve(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 临时用
     */
    @PostMapping(value = "/login")
    public ResponseEntity<Void> login(HttpServletRequest request, HttpServletResponse response) {
        LoginInfo loginInfo = new LoginInfo(1, 1, LoginInfo.UserType.PATIENT, "樟山", jwtUtils.getUserAgent(request));
        jwtUtils.createToken(loginInfo, request, response);
        return ResponseEntity.ok().build();
    }

    /**
     * 微信小程序用户绑定患者用户
     *
     * @param req
     * @return
     */
    @PostMapping(value = "/user/applet/bind")
    public ResponseEntity<PatientUserWxLoginRes> bindPatientUserWx(@Valid BindPatientUserWxReq req) {
        PatientUserWxLoginRes res = patientUserService.bindPatientUserWx(req);
        return ResponseEntity.ok(res);
    }


    /**
     * 获取患者小程序用户当前登录微信手机号
     *
     * @param req
     * @return
     */
    @GetMapping(value = "/user/applet/phone")
    public ResponseEntity<PlatLoginRes<?>> queryPatientUserPhoneWx(@Valid BindPatientUserWxReq req,
                                                                   HttpServletRequest request, HttpServletResponse response) {
        PlatLoginRes<?> res = patientUserService.queryPatientUserPhoneWx(req, request, response);
        return ResponseEntity.ok(res);
    }


    /**
     * 添加评价
     */
    @PostMapping("/user/appraise")
    public ResponseEntity<Void> addPatientUserAppraise(@Valid @RequestBody PatientUserAppraiseReq patientUserAppraiseReq) {
        patientUserAppraiseService.addPatientUserAppraise(patientUserAppraiseReq);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取我的评价
     */
    @GetMapping(value = "/user/appraise/page")
    public ResponseEntity<IPage<PatientUserAppraiseRes>> queryPatientUserAppraisePage(@Valid PageReq pageReq) {
        IPage<PatientUserAppraiseRes> res = patientUserAppraiseService.queryPatientUserAppraise(pageReq);
        return ResponseEntity.ok(res);
    }

    /**
     * 添加投诉
     */
    @PostMapping("/user/complaint")
    public ResponseEntity<Void> addPatientUserComplaint(@Valid @RequestBody PatientUserComplaintReq patientUserComplaintReq) {
        patientUserComplaintService.addPatientUserComplaint(patientUserComplaintReq);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询投诉
     */
    @GetMapping(value = "/user/complaint/page")
    public ResponseEntity<IPage<PatientUserComplaintRes>> queryPatientUserComplaintPage(@Valid PageReq pageReq) {
        IPage<PatientUserComplaintRes> res = patientUserComplaintService.queryPatientUserComplaintPage(pageReq);
        return ResponseEntity.ok(res);
    }

    /**
     * 添加患者用户地址数据
     */
    @PostMapping("/add/addr")
    public ResponseEntity<Void> addUserAddress(@Valid @RequestBody PatientUserAddressReq patientUserAddressReq) {
        patientUserAddressService.addUserAddress(patientUserAddressReq);
        return ResponseEntity.ok().build();
    }

    /**
     * 编辑用户地址
     */
    @PostMapping("/update/addr")
    public ResponseEntity<Void> updateUserAddress(@Valid @RequestBody PatientUserAddressUpdate patientUserAddressUpdate) {
        patientUserAddressService.updateUserAddress(patientUserAddressUpdate);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询用户地址列表
     */
    @GetMapping("/query/addr/page")
    public ResponseEntity<IPage<PatientUserAddress>> queryUserAddressPage(@Valid PageReq pageReq) {
        IPage<PatientUserAddress> res = patientUserAddressService.queryUserAddressPage(pageReq);
        return ResponseEntity.ok(res);
    }

    /**
     * 删除用户地址列表
     */
    @PostMapping("/update/addr/status")
    public ResponseEntity<Void> updatePatientUserAddress(@RequestParam("id") Integer id) {
        patientUserAddressService.updatePatientUserAddress(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询用户本人信息信息
     */
    @GetMapping("/query/med/info")
    public ResponseEntity<PatientMedCard> queryPatientInfo() {
        PatientMedCard patientMedCard = patientMedRankService.queryPatientInfo();
        return ResponseEntity.ok(patientMedCard);
    }

    @Autowired
    private RabbitSend rabbitSend;

    /**
     * 123
     */
    @PostMapping("/test/rabbit")
    public ResponseEntity<Void> test(@NotBlank(message = "time不能为空") String time,
                                     @NotBlank(message = "id不为为空") String id) {
        log.info(id);
        DelayMqVo<String> delayMqVo = new DelayMqVo<>();
        delayMqVo.setT(id);
        delayMqVo.setType(DelayMqVo.DelayMqVoType.CONSULT_AUTO_COMPLETE);
//        //毫秒级 10min消息
//        long ms = 1000 * 60;
        rabbitSend.sendMessageDelay(MQConfig.DELAY_EXCHANGE, MQConfig.DELAY_KEY, delayMqVo, Long.valueOf(time));
        return ResponseEntity.ok().build();
    }
}
